<template>
  <div class="clock-card">
    <div class="clock-time">{{ time }}</div>
    <div class="clock-date">{{ date }}</div>
    <div class="clock">
      <div class="clock-hour" :style="hourStyle"></div>
      <div class="clock-minute" :style="minuteStyle"></div>
      <div class="clock-second" :style="secondStyle"></div>
      <!-- 中心点 -->
      <div class="clock-center"></div>
      <div class="clock-number">
        <span class="clock-number-item" 
          :style="{ transform: `rotate(${item * 30}deg) translate(-10%, -55px) rotate(-${item * 30}deg)` }"
          v-for="item in 12" :key="item">{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import dayjs from 'dayjs'

const time = ref('')
const date = ref('')
const now = ref(dayjs())

function updateTime() {
  now.value = dayjs()
  time.value = now.value.format('HH:mm:ss')
  const weekArr = ['日', '一', '二', '三', '四', '五', '六']
  date.value = `${now.value.format('YYYY-MM-DD')} 星期${weekArr[now.value.day()]}`
}

let rafId = null
function loop() {
  updateTime()
  // rafId = requestAnimationFrame(loop)
}

function handleVisibility() {
  if (document.visibilityState === 'visible') {
    updateTime()
  }
}

onMounted(() => {
  // loop()
  document.addEventListener('visibilitychange', handleVisibility)
})

onUnmounted(() => {
  cancelAnimationFrame(rafId)
  document.removeEventListener('visibilitychange', handleVisibility)
})

// 指针角度计算（如需表盘可直接用这些变量）
const hourStyle = computed(() => ({
  transform: `rotate(${((now.value.hour() % 12) * 30 + now.value.minute() * 0.5)}deg)`
}))
const minuteStyle = computed(() => ({
  transform: `rotate(${now.value.minute() * 6}deg)`
}))
const secondStyle = computed(() => ({
  transform: `rotate(${now.value.second() * 6}deg)`
}))
</script>

<style scoped lang="scss">
.clock-card {
  background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(80, 80, 180, 0.12);
  padding: 24px 0 16px 0;
  margin-bottom: 20px;
  text-align: center;
  color: #333;
  user-select: none;
  transition: box-shadow 0.3s;
  margin-top: 10px;

  &:hover {
    box-shadow: 0 8px 32px rgba(80, 80, 180, 0.38);
  }

  .clock-time {
    font-size: 2.5rem;
    font-weight: bold;
    letter-spacing: 2px;
    margin-bottom: 8px;
    color: #4b3ca7;
    text-shadow: 0 2px 8px #fff8;
  }

  .clock-date {
    font-size: 1.1rem;
    color: #555;
    letter-spacing: 1px;
  }

  .clock {
    margin: 10px auto;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    border: 6px solid #000;
    position: relative;

    .clock-hour,
    .clock-minute,
    .clock-second {
      background: #4b3ca7;
      position: absolute;
    }

    .clock-center {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: #000;

    }

    .clock-hour {

      height: 20%;
      left: 48.75%;
      top: 30%;
      transform-origin: 50% 100%;
      width: 2.5%;
    }

    .clock-minute {
      height: 40%;
      left: 49%;
      top: 10%;
      transform-origin: 50% 100%;
      width: 2%;
    }

    .clock-second {
      height: 45%;
      left: 50%;
      top: 14%;
      transform-origin: 50% 80%;
      width: 1%;
      z-index: 8;
    }

    .clock-number {
      .clock-number-item {
        position: absolute;
        left:45%;
        top: 45%;
        font-size: 1rem;
        font-weight: 700;
        // text-align: center;
        // transform: translate(-50%, -50%); 
        // width: 50%;
      }
    }
  }
}
</style>